using System.Collections;
using UnityEngine;

public class BasicInput : MonoBehaviour
{
	private float HB = -20f;

	private RCC_CarControllerV3 RCCcar;

	public static float Forward;

	public static float Back;

	public static float LeftRight;

	public static float JoysticLeftRight;

	public static float HandBrake;

	public static float Boost;

	public static float carJump;

	public static float tankTurn;

	private CarBehavior car;

	private bool espEnabled;

	private bool tcsEnabled;

	private bool absEnabled;

	private bool steeringHelperEnabled;

	private float axisInputSteer;

	private float axisInputForward;

	private float axisInputBack;

	private bool needEnableHelpersCorutine;

	private bool enableHelpersCorutineStarted;

	public bool drift;

	public float shiftAngel;

	public float cameraControlsSensitivity = 1f;

	private float maxJumpRotation = 5f;

	private float currentJumpRotationX;

	private float currentJumpRotationY;

	private float resetTime;

	private bool autoGetOnWheels;

	private bool getOnWheels;

	public static void Reset()
	{
		Forward = 0f;
		Back = 0f;
		LeftRight = 0f;
		HandBrake = 0f;
		Boost = 0f;
		carJump = 0f;
	}

	private void Start()
	{
		car = GetComponent<CarBehavior>();
		RCCcar = GetComponent<RCC_CarControllerV3>();
		espEnabled = RCCcar.ESP;
		tcsEnabled = RCCcar.TCS;
		absEnabled = RCCcar.ABS;
		steeringHelperEnabled = RCCcar.steeringHelper;
	}

	public IEnumerator EnableHelpers()
	{
		yield return new WaitForSeconds(0.25f);
		if (enableHelpersCorutineStarted)
		{
			enableHelpersCorutineStarted = false;
			if (espEnabled && tcsEnabled)
			{
				RCCcar.TCS = true;
			}
			if (absEnabled)
			{
				RCCcar.ABS = true;
			}
			if (steeringHelperEnabled)
			{
				RCCcar.steeringHelper = true;
			}
			drift = false;
			RCC_Settings.instance.behaviorType = RCC_Settings.BehaviorType.Custom;
			RCC_GroundMaterials.instance.frictions[0].sidewaysStiffness = 2f;
			RCC_GroundMaterials.instance.frictions[0].slip = 0.25f;
		}
	}

	private void FixedUpdate()
	{
		if (GameController.instance == null || GameController.instance.carScript == null || GameController.instance.carScript != car || (GameController.instance.curTypeGame == TypeGame.race && !RaceController.instance.enableMove))
		{
			return;
		}
		if (HandBrake > 0f)
		{
			if (espEnabled)
			{
				RCCcar.ESP = false;
			}
			if (tcsEnabled)
			{
				RCCcar.TCS = false;
			}
			if (absEnabled)
			{
				RCCcar.ABS = false;
			}
			if (steeringHelperEnabled)
			{
				RCCcar.steeringHelper = false;
			}
			RCCcar.handbrakeInput = HB;
			if (!needEnableHelpersCorutine)
			{
				StopCoroutine(EnableHelpers());
			}
			needEnableHelpersCorutine = true;
			drift = true;
			RCC_Settings.instance.behaviorType = RCC_Settings.BehaviorType.Drift;
			RCC_GroundMaterials.instance.frictions[0].sidewaysStiffness = 2.8f;
			RCC_GroundMaterials.instance.frictions[0].slip = 10f;
		}
		else
		{
			if (needEnableHelpersCorutine && !enableHelpersCorutineStarted)
			{
				needEnableHelpersCorutine = false;
				enableHelpersCorutineStarted = true;
				StopCoroutine(EnableHelpers());
				StartCoroutine(EnableHelpers());
			}
			RCCcar.handbrakeInput = 0f;
		}
		if (axisInputForward != 0f)
		{
			RCCcar.gasInput = axisInputForward;
		}
		else
		{
			RCCcar.gasInput = Forward;
		}
		if (axisInputBack != 0f)
		{
			RCCcar.brakeInput = axisInputBack;
		}
		else
		{
			RCCcar.brakeInput = Back;
		}
		float num = 0f;
		Vector3 vector = new Vector3(RCCcar.transform.forward.x, 0f, RCCcar.transform.forward.z);
		Vector3 b = new Vector3(TPSCamera.instance.transform.forward.x, 0f, TPSCamera.instance.transform.forward.z);
		num = ((!TPSCamera.instance.isRotating) ? 0f : ((!(Vector3.Dot(new Vector3(RCCcar.transform.forward.x, 0f, RCCcar.transform.forward.z), new Vector3(TPSCamera.instance.transform.forward.x, 0f, TPSCamera.instance.transform.forward.z)) > 0f)) ? (0f - TPSCamera.SignedAngle(-vector, b, Vector3.up)) : TPSCamera.SignedAngle(vector, b, Vector3.up)));
		float num2 = Mathf.Clamp(num, -60f, 60f) / 60f * 2f;
		if (settings.typeControlerCar == TypeControlCar.camera)
		{
			if (Mathf.Abs(num) > shiftAngel)
			{
				LeftRight = num2;
			}
			else
			{
				LeftRight = 0f;
			}
		}
		if (RCCcar.vehicleType == VehicleType.Tank)
		{
			LeftRight = JoysticLeftRight + num2;
		}
		float num3 = 0f;
		num3 = ((axisInputSteer == 0f) ? LeftRight : axisInputSteer);
		RCCcar.steerInput = num3;
		if (drift)
		{
			RCCcar.steerInput *= 2f;
		}
		RCCcar.tankTurnInput = tankTurn;
		if (tankTurn == 0f)
		{
			RCCcar.tankTurnInput = num2;
		}
		if (RCCcar.brakeInput > 0f)
		{
			car.ForcedBrake();
		}
		OnBoost();
		CarJump();
		JumpRotation();
		GetOnWheels();
		if (GameController.instance.curTypeGame != TypeGame.race && RCCcar.gasInput.Equals(0f) && RCCcar.brakeInput.Equals(0f) && RCCcar.steerInput.Equals(0f) && !drift)
		{
			RCCcar.handbrakeInput = 0.15f;
		}
	}

	private void OnBoost()
	{
		if (Boost > 0f)
		{
			RCCcar.gasInput = 1f;
		}
		if (car.currentBoostAmount <= 0f)
		{
			car.currentBoostAmount = 0f;
		}
		if (Boost > 0f && car.currentBoostAmount > 0f && RCCcar.engineRunning)
		{
			car.onBoost = true;
			car.currentBoostAmount -= Time.fixedDeltaTime * 10f;
		}
		else
		{
			car.onBoost = false;
		}
	}

	private void CarJump()
	{
		if (carJump > 0f && !car.onJump && RCCcar.engineRunning)
		{
			carJump = 0f;
			car.rigidCar.AddForce(car.transform.up * car.jumpForce, ForceMode.Impulse);
			CarJumpButton.instance.OnJump();
		}
	}

	private void JumpRotation()
	{
		if (!RCCcar.engineRunning || !car.onJump || car.onBoost || !car.isFlaing)
		{
			currentJumpRotationX = 0f;
			currentJumpRotationY = 0f;
			return;
		}
		float b = 0f;
		float b2 = 0f;
		if (RCCcar.gasInput > 0f)
		{
			b = maxJumpRotation;
		}
		else if (RCCcar.brakeInput > 0f)
		{
			b = 0f - maxJumpRotation;
		}
		if (RCCcar.steerInput > 0f)
		{
			b2 = 0f - maxJumpRotation;
		}
		else if (RCCcar.steerInput < 0f)
		{
			b2 = maxJumpRotation;
		}
		currentJumpRotationX = Mathf.Lerp(currentJumpRotationX, b2, Time.deltaTime * 10f);
		currentJumpRotationY = Mathf.Lerp(currentJumpRotationY, b, Time.deltaTime * 10f);
		car.transform.Rotate(currentJumpRotationY, 0f, currentJumpRotationX);
	}

	private void GetOnWheels()
	{
		if (!car.onJump && RCCcar.speed < 4f && RCCcar.transform.eulerAngles.z < 300f && RCCcar.transform.eulerAngles.z > 60f)
		{
			getOnWheels = true;
			resetTime += Time.unscaledDeltaTime;
			if (resetTime > 3f)
			{
				autoGetOnWheels = true;
			}
		}
		if (!autoGetOnWheels && RCCcar.gasInput == 0f && RCCcar.brakeInput == 0f && RCCcar.steerInput == 0f)
		{
			getOnWheels = false;
			return;
		}
		float zAngle = maxJumpRotation;
		if (RCCcar.brakeInput > 0f || RCCcar.steerInput < 0f)
		{
			zAngle = 0f - maxJumpRotation;
		}
		if (getOnWheels)
		{
			car.transform.Rotate(0f, 0f, zAngle);
			if (!car.isFlaing)
			{
				getOnWheels = false;
				autoGetOnWheels = false;
				resetTime = 0f;
			}
		}
	}
}
